package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"github.com/zhaocy/fyne-theme"
)

func main() {
	myApp := app.New()
	myApp.Settings().SetTheme(themex.Themex())
	myWin := myApp.NewWindow("自定义widget")

	// 只有文本的按钮
	btn := widget.NewButton("NORMAL", func() {})

	// 带有图标的按钮
	btn2 := widget.NewButtonWithIcon("ICON", theme.InfoIcon(), func() {})

	// 左对齐
	btn3 := &widget.Button{
		Alignment: widget.ButtonAlignLeading,
		Text:      "左对齐",
	}

	// 右对齐
	btn4 := &widget.Button{
		Alignment: widget.ButtonAlignTrailing,
		Text:      "右对齐",
	}

	btn5 := widget.NewButton("禁用", func() {})
	btn5.Disable()

	// pop menu
	btn6 := newContextMenuButton("按钮", fyne.NewMenu("",
		fyne.NewMenuItem("复制", func() { fmt.Println("pop菜单") }),
		fyne.NewMenuItemSeparator(),
		fyne.NewMenuItem("菜单2", func() {}),
	))

	myWin.SetContent(container.NewVBox(btn, btn2, btn3, btn4, btn5, btn6))
	myWin.Resize(fyne.NewSize(550, 450))
	myWin.ShowAndRun()
}

type contextMenuButton struct {
	widget.Button
	menu *fyne.Menu
}

func (b *contextMenuButton) Tapped(e *fyne.PointEvent) {
	widget.ShowPopUpMenuAtPosition(b.menu,
		fyne.CurrentApp().Driver().CanvasForObject(b),
		e.AbsolutePosition)
}

func newContextMenuButton(label string, menu *fyne.Menu) *contextMenuButton {
	b := &contextMenuButton{menu: menu}
	b.Text = label

	b.ExtendBaseWidget(b)
	return b
}
